<template>
  <page-header-wrapper :title="false">
    <a-card title="官方状态查询">
      <a-tabs type="card" v-model="tab" @change="onTabs">
        <a-tab-pane v-for="(item,index) in categoryOpts" :key="index" :tab="item"></a-tab-pane>
      </a-tabs>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务编号">
                <a-input v-model="queryParam['filter[business_no]']" placeholder="输入业务编号" />
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="客户姓名">
                  <a-input v-model="queryParam['filter[customer_name]']" placeholder="输入客户姓名" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="更新时间">
                  <a-range-picker v-model="queryParam['created_at_start']" @change="onChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="进展状态">
                  <a-select
                    show-search
                    :filter-option="filterOption"
                    v-model="queryParam['filter[state_id]']"
                    placeholder="选择进展状态"
                  >
                    <a-select-option v-for="(item,index) in stateOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.cbs_id"
          :columns="stateColumns"
          :data="stateData"
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        >
          <template slot="title">
            <a-button :loading="loading" :disabled="selectedRowKeys.length === 0" size="small" type="primary" @click="onBatch">批量下载</a-button>
          </template>
          <template slot="sb_reg_no" slot-scope="text,record">
            {{text}}{{record.zl_no}}
          </template>
          <template slot="state_name" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
          </template>
          <template slot="filename" slot-scope="text,record">
            <a-button type="link" icon="pdf" @click="preview(record.file)">{{text}}</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { STable } from '@/components'
  import businessColumns from '@/columns/business'
  import { downloadState, getState } from '@/api/business'

  export default {
    components: {
      STable
    },
    name: 'StateView',
    data () {
      return {
        tab: '10',
        loading: false,
        advanced: true,
        queryParam: {},
        selectedRowKeys: [],
        stateColumns: businessColumns.state,
        stateData: parameters => getState(Object.assign(parameters, this.queryParam)).then(res => {
          this.categoryOpts = res.result.categoryOpts
          this.stateOpts = res.result.businessStateOpts
          return res.result
        }),
        categoryOpts: {},
        stateOpts: {}
      }
    },
    created () {
      this.queryParam['filter[category]'] = '10'
    },
    methods: {
      toggleAdvanced () {
        this.advanced = true
      },
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      preview (url) {
        window.open(url)
      },
      onChange (date, dateString) {
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      onBatch () {
        const app = this
        const data = {}
        app.loading = true
        data['cbs_ids'] = app.selectedRowKeys
        downloadState(data).then(res => {
          if (res.status === 0) {
            window.open(res.result.fileUrl)
            app.$info({
              title: '下载成功'
            })
          } else {
            app.$warning({
              title: res.msg
            })
          }
        }).finally(() => {
          app.loading = false
        })
      },
      onTabs (key) {
        this.queryParam['filter[category]'] = key
        this.$refs.table.refresh(true)
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      }
    }
  }
</script>

<style scoped>

</style>
